//
//  DoctorDetailTeamCell.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/4/10.
//  Copyright © 2020 QDSG. All rights reserved.
//

import UIKit

protocol DoctorDetailTeamCellDelagate: AnyObject {
    func doctorDetailTeamCellDidSelectItem(indexPath: IndexPath)
}

class DoctorDetailTeamCell: UITableViewCell {
    
    var viewModel: DoctorDetailViewModel?
    
    weak var delegate: DoctorDetailTeamCellDelagate?
    
    func updateData(vm: DoctorDetailViewModel) {
        viewModel = vm
        collectionView.reloadData()
    }
    
    @IBOutlet weak var collectionView: UICollectionView! {
        didSet {
            collectionView.register(DoctorDetailTeamCvCell.nibForCell(),
                                    forCellWithReuseIdentifier: DoctorDetailTeamCvCell.reuseIdentifier())
        }
    }
    
}

extension DoctorDetailTeamCell: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if let vm = viewModel {
            return vm.doctorTeamListDto.count
        }
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DoctorDetailTeamCvCell.reuseIdentifier(),
                                                      for: indexPath) as! DoctorDetailTeamCvCell
        if let vm = viewModel {
            cell.updateData(vm: vm.teamViewModel(for: indexPath))
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 212, height: 130)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 16
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        delegate?.doctorDetailTeamCellDidSelectItem(indexPath: indexPath)
    }
}
